package com.mathdroid.gui.main.talents;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.net.URL;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

import com.mathdroid.model.Model;
import com.mathdroid.model.enums.ETalentTree;
import com.mathdroid.model.talents.Talent;

/**
 * This panel contains all talent buttons for one tree.
 */
public class TalentTree extends JPanel {
	/**
	 * Default UID.
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * The width of one talent button.
	 */
	private static final int TALENT_WIDTH = 50;
	
	/**
	 * The height of one talent button.
	 */
	private static final int TALENT_HEIGHT = 50;
	
	/**
	 * The space between talents and the panel border.
	 */
	private static final int TALENT_SPACING = 10;
	
	/**
	 * The number of talent button rows.
	 */
	private static final int TALENT_ROWS = 7;
	
	/**
	 * The number of talent button columns.
	 */
	private static final int TALENT_COLS = 4;
	
	/**
	 * The font used to show the talent points.
	 */
	private static final Font FONT = new Font("Arial", Font.BOLD, 12);
	
	/**
	 * The color of the points.
	 */
	private static final Color DARK_BLUE = new Color(0, 62, 104);
	
	/**
	 * The background image of this tree.
	 */
	private Image background = null;
	
	/**
	 * The name/enumeration of this talent tree representation.
	 */
	private ETalentTree tree = null;
	
	/**
	 * The list of talent buttons.
	 */
	private ArrayList<TalentButton> buttons = new  ArrayList<TalentButton>();
	
	/**
	 * Constructor.
	 * @param talenttree The name/enumeration of this talent tree.
	 */
	public TalentTree(final ETalentTree talenttree) {
		super();
		final int red = 200;
		final int green = 221;
		final int blue = 242;
		
		URL url = TalentButton.class.getClass().getResource(
				"/com/mathdroid/gui/images/" 
				+ talenttree.getFileName());
		this.background = new ImageIcon(url).getImage();
		
		this.setBackground(new Color(red, green, blue));
		this.tree = talenttree;
		this.setLayout(null);
		Dimension d = new Dimension(
			TALENT_SPACING + TALENT_COLS * (TALENT_WIDTH + TALENT_SPACING), 
			TALENT_SPACING + TALENT_ROWS * (TALENT_HEIGHT + TALENT_SPACING));
		this.setPreferredSize(d);
		this.addButtons();

		for (Talent t 
				: Model.getProfile().getSpecTemplate()
				.getTree(this.tree).values()) {
			buttons.get(t.getGuiSlot()).setTalent(t);
		}
	}
	
	/**
	 * Adds all 28 talent buttons to the panel.
	 */
	private void addButtons() {
		for (int y = 0; y < TalentTree.TALENT_ROWS; y++) {
			for (int x = 0; x < TalentTree.TALENT_COLS; x++) {
				TalentButton b = new TalentButton(null);
				b.setBounds(
						TALENT_SPACING + x * (TALENT_SPACING + TALENT_WIDTH), 
						TALENT_SPACING + y * (TALENT_HEIGHT + TALENT_SPACING), 
						TALENT_HEIGHT, 
						TALENT_HEIGHT);
				this.add(b);
				this.buttons.add(b);
			}
		}
	}
	
	@Override
	public final void paintComponent(final Graphics g) {
		super.paintComponent(g);
		final int pointsLeft = 
			Model.getProfile().getSpecTemplate().getMaxPoints() 
			- Model.getProfile().getSpecTemplate().getTotalPointsSpent();
		
		g.drawImage(this.background, 0, 0, this);
		
		g.setFont(TalentTree.FONT);	
		g.setColor(TalentTree.DARK_BLUE);
		
		g.drawString(pointsLeft + " Points left", TALENT_SPACING, 
						TALENT_SPACING 
						+ TALENT_ROWS * (TALENT_HEIGHT + TALENT_SPACING) 
						+ TALENT_SPACING);
	}
}
